package logic

import (
	"context"
	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"
	"house/models/mysql"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type ResListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewResListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResListLogic {
	return &ResListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ResListLogic) ResList(in *houserpc.ResListRequest) (*houserpc.ResListResponse, error) {
	// todo: add your logic here and delete this line
	var a []mysql.ResData
	mysql.DB.Debug().Model(&mysql.Res{}).Order("res.res_time asc").
		Select("res.*,users.name,users.mobile,houses.title,houses.area,houses.city").
		Joins("left join houses on res.house_id = houses.id").
		Joins("left join users on houses.user_id = users.id").
		Find(&a)

	var b []*houserpc.ResList
	for _, c := range a {
		list := houserpc.ResList{
			ResId:     int64(c.ID),
			UserName:  c.Name,
			Mobile:    c.Mobile,
			HouseName: c.Title,
			City:      c.City,
			Area:      int64(c.Area),
			ResTime:   c.ResTime.Format(time.DateTime),
		}
		b = append(b, &list)
	}
	return &houserpc.ResListResponse{
		List: b,
	}, nil
}
